package 设计模式.组合模式.safeBase;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Composite
 * @Description 抽象组合，透明实现，实现的父类的空方法,树节点
 * B.子节点实现
 */
public class Composite  extends Component {
    List<Component> mComponents;
    public Composite(String name) {
        super(name);
        mComponents = new ArrayList<Component>();
    }

    @Override
    public String operation() {
        StringBuilder builder = new StringBuilder(this.name);
        for(Component component: this.mComponents){
            builder.append("\n");
            builder.append(component.operation());
        }
        return builder.toString();
    }

    public boolean addChild(Component component) {
        return this.mComponents.add(component);
    }


    public boolean removeChild(Component component) {
        return this.mComponents.remove(component);
    }


    public Component getChild(int index) {
        return this.mComponents.get(index);
    }
}
